Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

WeakHashMap → Java WeakHashMap

WeakHashMap

Java WeakHashMap

WeakHashMap class in Java collection framework

The WeakHashMap class in Java implements the Map interface and provides a specialized key-value pair storage mechanism. It uses weak references for keys, allowing the garbage collector (GC) to reclaim memory associated with keys even if they're still in the WeakHashMap. This behavior is distinct from standard HashMap where keys are strongly referenced and remain in memory until explicitly removed.

Characteristics of WeakHashMap:

Weak References: Keys in a WeakHashMap are held using weak references. This means the GC can remove a key if there are no other strong references to it, even if the key is still in the map. Automatic Removal: When a key is garbage collected, the corresponding key-value pair is automatically removed from the WeakHashMap. Unpredictable Iteration: Since keys can be removed by the GC at any time, iterating over a WeakHashMap may not provide a consistent view of the map's contents. Concurrent modifications during iteration can also lead to ConcurrentModificationException.

Uses of WeakHashMap:

Caching: When caching objects and you want to avoid memory leaks if the cached object is no longer referenced elsewhere in your program. The WeakHashMap automatically removes the cache entry when the object is no longer in use. Object Listeners: When associating listeners with objects and you want to avoid preventing the objects from being garbage collected as long as the listener is registered. The WeakHashMap allows the listener to be garbage collected independently.

Initializing and Declaring WeakHashMap:

Declaration
Declaring syntax WeakHashMap<KeyType, ValueType> mapName;
Explanation : WeakHashMap: This specifies the class you're using. <KeyType, ValueType>: Placeholders for the data types used for keys and values in the map. Keys must be objects and not primitive data types. mapName: The name for your reference variable.
Initialization: Creating an empty WeakHashMap:
Creating empty weakhashmap WeakHashMap<String, Integer> resourceCache = new WeakHashMap<>();

Adding Key-Value Pairs: ✦ The put(K key, V value) method is used to add key-value pairs to the WeakHashMap. ✦ Remember, keys are weakly referenced, so the GC can remove them at any time.

Example

Simple example of weakhashmap in java import java.util.WeakHashMap; class Main { public static void main(String[] args) { // Creating WeakHashMap of numbers WeakHashMap<String, Integer> numbers = new WeakHashMap<>(); String two = new String("Two"); Integer twoValue = 2; String four = new String("Four"); Integer fourValue = 4; // Inserting elements numbers.put(two, twoValue); numbers.put(four, fourValue); System.out.println("WeakHashMap: " + numbers); // Make the reference null two = null; // Perform garbage collection System.gc(); System.out.println("WeakHashMap after garbage collection: " + numbers); } }

Output

WeakHashMap: {Four=4, Two=2} WeakHashMap after garbage collection: {Four=4}

Remember:

Since WeakHashMap relies on garbage collection behavior, the exact timing of key removal is not guaranteed. It's crucial to ensure your values in the WeakHashMap have strong references elsewhere to prevent them from being garbage collected unintentionally.

Tutorials